namespace DotNetNuke.UI.WebControls
{
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Microsoft.VisualBasic;

    public class DNNTextSuggest : TextBox, IPostBackEventHandler, IClientAPICallbackEventHandler
    {
        private DNNNodeCollection m_objNodes;

        public event DNNDNNNodeClickHandler NodeClick;

        public event DNNTextSuggestEventHandler PopulateOnDemand;

        public DNNTextSuggest()
        {
            base.PreRender += new EventHandler(this.DNNTextSuggest_PreRender);
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute("sysimgpath", this.SystemImagesPath);
            if (Strings.Len(this.Target) > 0)
            {
                writer.AddAttribute("target", this.Target);
            }
            if (Strings.Len(this.TextSuggestCssClass) > 0)
            {
                writer.AddAttribute("tscss", this.TextSuggestCssClass);
            }
            if (Strings.Len(this.DefaultNodeCssClass) > 0)
            {
                writer.AddAttribute("css", this.DefaultNodeCssClass);
            }
            if (Strings.Len(this.DefaultChildNodeCssClass) > 0)
            {
                writer.AddAttribute("csschild", this.DefaultChildNodeCssClass);
            }
            if (Strings.Len(this.DefaultNodeCssClassOver) > 0)
            {
                writer.AddAttribute("csshover", this.DefaultNodeCssClassOver);
            }
            if (Strings.Len(this.DefaultNodeCssClassSelected) > 0)
            {
                writer.AddAttribute("csssel", this.DefaultNodeCssClassSelected);
            }
            if (Strings.Len(this.JSFunction) > 0)
            {
                writer.AddAttribute("js", this.JSFunction);
            }
            writer.AddAttribute("postback", ClientAPI.GetPostBackEventReference(this, "[TEXT]" + ClientAPI.COLUMN_DELIMITER + "Click"));
            if (ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.XMLHTTP))
            {
                writer.AddAttribute("callback", ClientAPI.GetCallbackEventReference(this, "'[TEXT]'", "this.callBackSuccess", "this", "this.callBackFail", "this.callBackStatus"));
            }
            if (Strings.Len(this.CallbackStatusFunction) > 0)
            {
                writer.AddAttribute("callbackSF", this.CallbackStatusFunction);
            }
            if (Strings.Len(this.JSFunction) > 0)
            {
                writer.AddAttribute("js", this.JSFunction);
            }
        }

        private void DNNTextSuggest_PreRender(object sender, EventArgs e)
        {
            this.RegisterClientScript();
            this.Page.RegisterRequiresPostBack(this);
        }

        public DNNNode FindNode(string strID)
        {
            return this.DNNNodes.FindNode(strID);
        }

        public DNNNode FindNodeByKey(string strKey)
        {
            return this.DNNNodes.FindNodeByKey(strKey);
        }

        protected override void LoadViewState(object state)
        {
            if (state != null)
            {
                object[] objArray = (object[]) state;
                if (objArray[0] != null)
                {
                    base.LoadViewState(RuntimeHelpers.GetObjectValue(objArray[0]));
                }
            }
        }

        public void LoadXml(string strXml)
        {
            this.m_objNodes = new DNNNodeCollection(strXml, "");
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.RegisterDNNVariableControl();
        }

        protected override void OnLoad(EventArgs e)
        {
        }

        public virtual void OnNodeClick(DNNTextSuggestEventArgs e)
        {
            if (this.NodeClick != null)
            {
                this.NodeClick(this, e);
            }
        }

        public string RaiseClientAPICallbackEvent(string eventArgument)
        {
            string[] strArray = Strings.Split(eventArgument, ClientAPI.COLUMN_DELIMITER, -1, CompareMethod.Binary);
            if (this.PopulateOnDemand != null)
            {
                this.PopulateOnDemand(this, new DNNTextSuggestEventArgs(this.DNNNodes, eventArgument));
            }
            return this.DNNNodes.ToXml();
        }

        public virtual void RaisePostBackEvent(string eventArgument)
        {
            string[] strArray = Strings.Split(eventArgument, ClientAPI.COLUMN_DELIMITER, -1, CompareMethod.Binary);
            if (strArray.Length > 1)
            {
                string sLeft = strArray[1];
                if (StringType.StrCmp(sLeft, "Click", false) == 0)
                {
                    DNNTextSuggestEventArgs e = new DNNTextSuggestEventArgs(this.DNNNodes, eventArgument);
                    this.OnNodeClick(e);
                }
            }
        }

        public void RegisterClientScript()
        {
            if (!this.IsDownLevel)
            {
                ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn_dom);
                ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn_xml);
                ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn_dom_positioning);
                ClientAPI.RegisterClientReference(this.Page, ClientAPI.ClientNamespaceReferences.dnn_xmlhttp);
                if (!ClientAPI.IsClientScriptBlockRegistered(this.Page, "dnn.controls.DNNTextSuggest.js"))
                {
                    ClientAPI.RegisterClientScriptBlock(this.Page, "dnn.controls.dnnTextSuggest.js", "<script src=\"" + this.TextSuggestScriptPath + "dnn.controls.dnnTextSuggest.js\"></script>");
                }
                ClientAPI.RegisterStartUpScript(this.Page, this.ClientID + "_startup", "<script>dnn.controls.initTextSuggest(dnn.dom.getById('" + this.ClientID + "'));</script>");
            }
        }

        public void RegisterDNNVariableControl()
        {
            if (((HtmlInputHidden) DotNetNuke.UI.Utilities.Globals.FindControlRecursive(this.Page, "__dnnVariable")) == null)
            {
                HtmlInputHidden child = new HtmlInputHidden();
                child.ID = "__dnnVariable";
                this.Controls.Add(child);
            }
        }

        protected override object SaveViewState()
        {
            object objectValue = RuntimeHelpers.GetObjectValue(base.SaveViewState());
            return new object[] { RuntimeHelpers.GetObjectValue(objectValue) };
        }

        protected override void TrackViewState()
        {
            base.TrackViewState();
        }

        [Bindable(true), PersistenceMode(PersistenceMode.Attribute)]
        public string CallbackStatusFunction
        {
            get
            {
                return StringType.FromObject(this.ViewState["CallbackStatusFunction"]);
            }
            set
            {
                this.ViewState["CallbackStatusFunction"] = value;
            }
        }

        public string ClientAPIScriptPath
        {
            get
            {
                return ClientAPI.ScriptPath;
            }
            set
            {
                ClientAPI.ScriptPath = value;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue("")]
        public string DefaultChildNodeCssClass
        {
            get
            {
                return StringType.FromObject(this.ViewState["DefaultChildNodeCssClass"]);
            }
            set
            {
                this.ViewState["DefaultChildNodeCssClass"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute), Bindable(true), DefaultValue("")]
        public string DefaultNodeCssClass
        {
            get
            {
                return this.CssClass;
            }
            set
            {
                this.CssClass = value;
            }
        }

        [Bindable(true), DefaultValue(""), PersistenceMode(PersistenceMode.Attribute)]
        public string DefaultNodeCssClassOver
        {
            get
            {
                return StringType.FromObject(this.ViewState["DefaultNodeCssClassOver"]);
            }
            set
            {
                this.ViewState["DefaultNodeCssClassOver"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.Attribute), DefaultValue(""), Bindable(true)]
        public string DefaultNodeCssClassSelected
        {
            get
            {
                return StringType.FromObject(this.ViewState["DefaultNodeCssClassSelected"]);
            }
            set
            {
                this.ViewState["DefaultNodeCssClassSelected"] = value;
            }
        }

        [PersistenceMode(PersistenceMode.InnerProperty), Browsable(true)]
        public DNNNodeCollection DNNNodes
        {
            get
            {
                if (this.m_objNodes == null)
                {
                    this.m_objNodes = new DNNNodeCollection(this.ClientID);
                }
                return this.m_objNodes;
            }
        }

        [Browsable(false)]
        public bool IsDownLevel
        {
            get
            {
                return (!ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.DHTML) || !ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.XML));
            }
        }

        public string JSFunction
        {
            get
            {
                return StringType.FromObject(this.ViewState["JSFunction"]);
            }
            set
            {
                this.ViewState["JSFunction"] = value;
            }
        }

        [Browsable(false)]
        public Collection SelectedDNNNodes
        {
            get
            {
                return this.DNNNodes.FindSelectedNodes();
            }
        }

        [DefaultValue("images/"), Description("Directory to find the images for the TextSuggest.  Need to have spacer.gif here!")]
        public string SystemImagesPath
        {
            get
            {
                if (Strings.Len(RuntimeHelpers.GetObjectValue(this.ViewState["SystemImagesPath"])) == 0)
                {
                    return "images/";
                }
                return StringType.FromObject(this.ViewState["SystemImagesPath"]);
            }
            set
            {
                this.ViewState["SystemImagesPath"] = value;
            }
        }

        [DefaultValue(""), Bindable(true)]
        public string Target
        {
            get
            {
                return StringType.FromObject(this.ViewState["Target"]);
            }
            set
            {
                this.ViewState["Target"] = value;
            }
        }

        [DefaultValue(""), Bindable(true), PersistenceMode(PersistenceMode.Attribute)]
        public string TextSuggestCssClass
        {
            get
            {
                return StringType.FromObject(this.ViewState["TextSuggestCssClass"]);
            }
            set
            {
                this.ViewState["TextSuggestCssClass"] = value;
            }
        }

        public string TextSuggestScriptPath
        {
            get
            {
                if (Strings.Len(RuntimeHelpers.GetObjectValue(this.ViewState["TextSuggestScriptPath"])) == 0)
                {
                    return this.ClientAPIScriptPath;
                }
                return StringType.FromObject(this.ViewState["TextSuggestScriptPath"]);
            }
            set
            {
                this.ViewState["TextSuggestScriptPath"] = value;
            }
        }

        public delegate void DNNDNNNodeClickHandler(object source, DNNTextSuggestEventArgs e);

        public delegate void DNNTextSuggestEventHandler(object source, DNNTextSuggestEventArgs e);
    }
}

